package com.mango.ch01;

import java.io.IOException;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;

public class TemperatureMapper extends Mapper<LongWritable, Text, DateTemperaturePair, IntWritable> {

	@Override
	protected void map(LongWritable key, Text value,
			Mapper<LongWritable, Text, DateTemperaturePair, IntWritable>.Context context)
			throws IOException, InterruptedException {
		// TODO Auto-generated method stub
		String tokens[] = value.toString().split(",");
		// yyyy=tokens[0],mm=tokens[1]
		// 所以yyyy-mm 格式需要拼接
		String yearMonth = tokens[0] + tokens[1];
		String day = tokens[2];
		int temperature = Integer.parseInt(tokens[3]);
		// 准备规约器需要的键
		DateTemperaturePair reducerKey = new DateTemperaturePair();
		reducerKey.setYearMonth(new Text(yearMonth));
		reducerKey.setDay(new Text(day));
		reducerKey.setTemperature(new IntWritable(temperature));
		// 发送到规约器 reduce ！！！注意 我们的键排序逻辑，此处不再是普通的健排序
		System.out.println("key=" + reducerKey + "  value=" + temperature);
		context.write(reducerKey, new IntWritable(temperature));
	}

}
